package com.group7.mall.service.impl;

import com.group7.mall.convert.CommentConverter;
import com.group7.mall.dao.*;
import com.group7.mall.dto.*;
import com.group7.mall.entity.*;
import com.group7.mall.resp.UserCommentsPageResponse;
import com.group7.mall.req.UserAddCommentRequest;
import com.group7.mall.req.UserCommentPageRequest;
import com.group7.mall.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private TbUserDao tbUserDao;

    @Resource
    private TbUserOrderDao tbUserOrderDao;

    @Resource
    private TbUserCommentDao tbUserCommentDao;

    @Resource
    private TbCommentDiscussionDao tbCommentDiscussionDao;

    @Resource
    private TbGoodsDao tbGoodsDao;

    @Resource
    private TbSellerDao tbSellerDao;

    @Override
    public List<CommentDiscussionDto> showCommentDiscussion(TbUserComment tbUserComment) {
        TbSeller tbSeller = tbSellerDao.queryById(tbGoodsDao.queryById(tbUserComment.getGoodsId()).getStoreId());
        String sellerId = tbSeller.getSellerId();
        List<TbCommentDiscussion> tbCommentDiscussionList = tbCommentDiscussionDao.queryUserCommentId(tbUserComment.getUserCommentId());
        List<CommentDiscussionDto> commentDiscussionDtoList = new ArrayList<>();
        tbCommentDiscussionList.forEach(tbCommentDiscussion -> {
            CommentDiscussionDto commentDiscussionDto = CommentConverter.INSTANCE.convertTbCommentDiscussionToCommentDiscussionDto(tbCommentDiscussion);
            if(tbCommentDiscussion.getUserOpenId().equals(sellerId)) {
                commentDiscussionDto.setIsSeller(1);
                commentDiscussionDto.setUserImage(tbSeller.getStoreImage());
                commentDiscussionDto.setUserName(tbSeller.getStoreName());
            } else {
                TbUser tbUser = tbUserDao.queryByUserOpenId(tbCommentDiscussion.getUserOpenId());
                commentDiscussionDto.setUserImage(tbUser.getUserImage());
                commentDiscussionDto.setUserName(tbUser.getUsername());
            }
            commentDiscussionDtoList.add(commentDiscussionDto);
        });
        return commentDiscussionDtoList;
    }

    @Override
    public UserCommentsPageResponse<UserCommentDto> showGoodsComments(UserCommentPageRequest userCommentPageRequest) {
        userCommentPageRequest.setPageStart((userCommentPageRequest.getPageNo() - 1) * userCommentPageRequest.getPageSize());
        List<TbUserComment> tbUserCommentList = tbUserCommentDao.queryAllByLimitForUser(userCommentPageRequest);
        for (TbUserComment x: tbUserCommentList) {
            System.out.println(x.toString());
        }
        List<UserCommentDto> userCommentDtoList = new ArrayList<>();
        for(TbUserComment x: tbUserCommentList) {
            UserCommentDto userCommentDto = CommentConverter.INSTANCE.convertTbUserCommentToUserCommentDto(x);
            TbUser tbUser = tbUserDao.queryByUserOpenId(x.getUserOpenId());
            TbUserOrder tbUserOrder = tbUserOrderDao.queryById(x.getOrderId());
//            if (x.getHavingAppendedComment() == 1) {
//                System.out.println("进行插入追评操作，需要为DTO新增字段");
//                TbUserComment tbUserAppendedComment = tbUserCommentDao.queryAppendedCommentByAppendedCommentId(x.getAppendedCommentId());
//                userCommentDto = this.setAppendedCommentToUserCommentDto(tbUserAppendedComment,userCommentDto);
//            }
            userCommentDto.setUserName(tbUser.getUsername());
            userCommentDto.setUserImage(tbUser.getUserImage());
            userCommentDto.setTypeInfo(tbUserOrder.getTypeInfo());
            // TODO 为userCommentDto增添评论下讨论
            List<TbCommentDiscussion> tbCommentDiscussionList = tbCommentDiscussionDao.queryUserCommentId(x.getUserCommentId());
            if (tbCommentDiscussionList != null && !tbCommentDiscussionList.isEmpty()) {
                userCommentDto.setHavingCommentDiscussion(1);
                // TODO 改成返回条数和预览即可
                userCommentDto.setCommentDiscussionNumber(tbCommentDiscussionList.size());
                userCommentDto.setCommentDiscussionPreview(tbCommentDiscussionList.get(0).getDiscussionContent());
                userCommentDto.setCommentDiscussionUserNamePreview(tbUserDao.queryByUserOpenId(tbCommentDiscussionList.get(0).getUserOpenId()).getUsername());
//                userCommentDto.setCommentDiscussion(tbCommentDiscussionList);
            } else {
                userCommentDto.setHavingCommentDiscussion(0);
            }
            System.out.println(userCommentDto.toString());
            userCommentDtoList.add(userCommentDto);
        }
        UserCommentsPageResponse<UserCommentDto> userCommentDtoUserCommentsPageResponse = new UserCommentsPageResponse<>();
        TbUserComment tbUserComment = new TbUserComment();
        tbUserComment.setGoodsId(userCommentPageRequest.getGoodsId());
        tbUserComment.setCommentSort(userCommentPageRequest.getCommentSort());
        tbUserComment.setHavingImage(userCommentPageRequest.getHavingImage());
        userCommentDtoUserCommentsPageResponse.setPageSize(userCommentPageRequest.getPageSize());
        userCommentDtoUserCommentsPageResponse.setCurrent(userCommentPageRequest.getPageNo());
        userCommentDtoUserCommentsPageResponse.setRecords(userCommentDtoList);
        userCommentDtoUserCommentsPageResponse.setTotal(tbUserCommentDao.count(tbUserComment));
        System.out.println(tbUserCommentDao.count(tbUserComment));
        return userCommentDtoUserCommentsPageResponse;
    }

    @Override
    public TbCommentDiscussion addCommentDiscussion(TbCommentDiscussion tbCommentDiscussion) {
        tbCommentDiscussionDao.insert(tbCommentDiscussion);
        return tbCommentDiscussion;
    }

    @Override
    public int insert(UserAddCommentRequest userAddCommentRequest) {
        TbUserComment tbUserComment = CommentConverter.INSTANCE.convertUserAddCommentRequestToTbUserComment(userAddCommentRequest);
        TbUserOrder tbUserOrder = tbUserOrderDao.queryById(tbUserComment.getOrderId());
        tbUserOrder.setOrderLock(0);
        tbUserOrder.setIsFinished(1);
        tbUserOrder.setFinishedTime(tbUserComment.getCreatedTime());
        tbUserComment.setGoodsId(tbUserOrder.getGoodsId());
        tbUserComment.setStoreId(tbUserOrder.getStoreId());
        // TODO 评论价值待解决
        tbUserComment.setCommentValue(0);
        if(tbUserComment.getCommentImage1() != null) {
            tbUserComment.setHavingImage(1);
//            tbUserComment.setCommentSort(4);
        } else {
            tbUserComment.setHavingImage(0);
        }
        System.out.println(tbUserComment);
        tbUserOrderDao.update(tbUserOrder);
        return tbUserCommentDao.insert(tbUserComment);
    }

//    public UserCommentDto setAppendedCommentToUserCommentDto(TbUserComment tbUserAppendedComment, UserCommentDto userCommentDto) {
//        userCommentDto.setAfterAppendedDays(tbUserAppendedComment.getAfterAppendedDays());
//        userCommentDto.setHavingAppendedComment(tbUserAppendedComment.getHavingAppendedComment());
//        userCommentDto.setAppendedCommentContent(tbUserAppendedComment.getCommentContent());
//        userCommentDto.setHavingAppendedImage(tbUserAppendedComment.getHavingImage());
//        userCommentDto.setAppendedCommentImage1(tbUserAppendedComment.getCommentImage1());
//        userCommentDto.setAppendedCommentImage2(tbUserAppendedComment.getCommentImage2());
//        userCommentDto.setAppendedCommentImage3(tbUserAppendedComment.getCommentImage3());
//        userCommentDto.setAppendedCommentImage4(tbUserAppendedComment.getCommentImage4());
//        userCommentDto.setAppendedCommentImage5(tbUserAppendedComment.getCommentImage5());
//        return userCommentDto;
//    }
}
